<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Guru extends CI_Controller {

    function __construct() { //inisialisasi library dan helper yang perlu di load
        parent::__construct();
        $this->load->helper(array('form', 'url', 'file'));
        $this->load->library('pagination');
        $this->load->library('parser');
        $this->load->library('upload');
        $this->load->model('all');
        $log = $this->session->all_userdata();
        if (!isset($log) || $log['login'] != TRUE || $log['hak_akses'] != 2) {
            $this->logout();
            redirect('main');
        }
    }

    function trueLogin() { //cek sudah login
        $log = $this->session->all_userdata();
        if (!isset($log) || $log['login'] != TRUE || $log['hak_akses'] != 2) {
            $this->logout();
            redirect('main');
        }
    }

    function index() {
        $this->trueLogin();
        $this->load->view('guru');
        redirect('guru/home');
    }

    function home() {
        $this->trueLogin();
        $data['blog_title'] = 'Home';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->session->all_userdata();
        $data['user'] = $this->all->username($id);
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('the/home', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function profil() {
        $data['blog_title'] = 'Profil';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->session->all_userdata();
        if ($this->all->profil($id)) {
            $data['profil'] = $this->all->profil($id);
        } else {
            $data['profil'] = array();
        }
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/profil', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function profil_update() {
        $this->trueLogin();
        $id = $this->session->all_userdata();
        $input = array(
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('telepon'),
            'tmpt_lahir' => $this->input->post('tempat'),
            'tgl_lahir' => $this->input->post('tgl')
        );
        $this->all->profil_update($id, $input);
        redirect('guru/profil');
    }

    function jadwal() {
        $this->trueLogin();
        $data['blog_title'] = 'Jadwal';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->session->all_userdata();
        if ($this->all->jadwal_mengajar($id)) {
            $data['jadwal'] = $this->all->jadwal_mengajar($id);
        } else {
            $data['jadwal'] = array();
        }
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/jadwal', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function nilai() {
        $this->trueLogin();
        $data['blog_title'] = 'Profil';
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->session->all_userdata();
        if ($this->all->nilai($id)) {
            $data['nilai'] = $this->all->nilai($id);
        } else {
            $data['nilai'] = array();
        }
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/nilai', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function nilai_ubah() {
        $this->trueLogin();
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->uri->segment(3);
        if ($this->all->get_nilai($id)) {
            $data['nilai'] = $this->all->get_nilai($id);
        } else {
            $data['nilai'] = array();
        }
        $data['blog_title'] = 'Input Nilai';
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/nilai_ubah', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function nilai_update() {
        $this->trueLogin();
        $nip = $this->session->all_userdata();
        $where = array(
            'nis' => $this->input->post('nis'),
            'mapel_id' => $this->input->post('mapel'),
            'semester_id' => $this->input->post('semester'),
            'tahun_id' => date('Y'),
            'nip' => $nip['id']
        );
        $input['nilai'] = $this->input->post('nilai');
        $this->all->nilai_update($where, $input);
        redirect('guru/nilai');
    }

    function materi() {
        $this->trueLogin();
        $this->load->helper('download');
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        $id = $this->session->all_userdata();
        if ($this->all->materi($id)) {
            $data['materi'] = $this->all->materi($id);
        } else {
            $data['materi'] = array();
        }
        $data['blog_title'] = 'Materi';
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/materi', $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function materi_upload() {
        $this->trueLogin();
        $nip = $this->session->all_userdata();
        if ($this->all->social()) {
            $data['social'] = $this->all->social();
        } else {
            $data['social'] = array();
        }
        if ($this->all->materi_mapel($nip)) {
            $data['mapel'] = $this->all->materi_mapel($nip);
        } else {
            $data['mapel'] = array();
        }
        if ($this->all->materi_semester($nip)) {
            $data['semester'] = $this->all->materi_semester($nip);
        } else {
            $data['semester'] = array();
        }
        if ($this->all->materi_tahun($nip)) {
            $data['tahun'] = $this->all->materi_tahun($nip);
        } else {
            $data['tahun'] = array();
        }
        $data['blog_title'] = 'Materi Upload';
        $this->parser->parse('the/header', $data);
        $this->load->view('the/open_tag');
        $this->load->view('the/open_tag_sidebar_left');
        $this->load->view('guru/menu');
        $this->load->view('the/close_tag_sidebar_left');
        $this->load->view('the/open_tag_news');
        $this->load->view('guru/materi_upload', array('error' => ' '), $data);
        $this->load->view('the/close_tag_news');
        $this->load->view('the/close_tag');
        $this->load->view('the/footer');
    }

    function materi_kirim() {
        $this->trueLogin();
        $config['upload_path'] = './materi/';
        $config['allowed_types'] = 'doc|docx|ppt|pptx|xls|xlsx|pdf|zip|rar';
        $config['max_size'] = '5000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            redirect('guru/materi_upload', $error);
        } else {
            $file = $this->upload->data();
            $nip = $this->session->all_userdata();
            $input = array(
                'judul' => $this->input->post('judul'),
                'mapel_id' => $this->input->post('mapel'),
                'semester_id' => $this->input->post('semester'),
                'tahun_id' => $this->input->post('tahun'),
                'nip' => $nip['id'],
                'file_path' => base_url() . 'materi/' . $file['file_name']
            );
            $data = $this->upload->data();
            $this->all->materi_upload($input, $data);
            redirect('guru/materi');
        }
    }

    function logout() {
        $this->session->sess_destroy();
        redirect();
    }

}

?>